package com.pks.datacenter.service.basic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pks.datacenter.entity.basic.SystemConfig;
import com.pks.datacenter.mapper.basic.SystemConfigMapper;
import com.pks.util.UtilString;
import com.pks.web.mybatis.service.BasicServiceImpl;

@Component("systemConfigService")
public class SystemConfigServiceImpl extends BasicServiceImpl<SystemConfig, Long, SystemConfigMapper> implements
		SystemConfigService {

	@Autowired
	private SystemConfigMapper systemConfigMapper;

	@Override
	protected SystemConfigMapper getMapper() {
		return systemConfigMapper;
	}

	@Override
	public Map<String, String> findMap() {
		Map<String, String> result = new HashMap<String, String>();
		List<SystemConfig> list = find(null, null, null);
		if (list != null && list.size() > 0) {
			for (SystemConfig c : list) {
				result.put(c.getType(), c.getVals());
			}
		}
		return result;
	}

	@Override
	public void update(String type, String vals) {
		if (UtilString.isNotBlank(type)) {
			synchronized (type) {
				List<SystemConfig> configList = find(null, initQbuilder("type", type), 1);
				if (configList != null && configList.size() > 0) {
					SystemConfig c = configList.get(0);
					c.setVals(vals);
					update(c);
				} else {
					SystemConfig obj = new SystemConfig();
					obj.setId(UtilString.getLongUUID());
					obj.setType(type);
					obj.setVals(vals);
					insert(obj);
				}
			}
		}
	}

}
